package amazon

import (
	"fmt"
	"gitee.com/wengo/go-mamba"
	http "github.com/Danny-Dasilva/fhttp"
	"github.com/peterbourgon/mergemap"
	"strings"
	"time"
)

type Methods string

//const (
//	POST      Methods = "post"
//	GET       Methods = "get"
//)

type Settings struct {
	JA3         string
	UserAgent   string
	BaseUri     string
	Header      map[string]interface{}
	Cookies     []*http.Cookie
	Debug       bool
	DebugLength int64
	Proxy       string
	ProxyType   string
	Client      *mamba.Client
}

type Amazon struct {
	//client   *mamba.Client
	//Document *goquery.Document
	//Response *mamba.Response
	Base
	//history []string
}

var defualtHeaders = map[string]interface{}{
	"Accept":          "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
	"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
	"Connection":      "keep-alive",
	"Accept-Encoding":           "gzip, deflate, br",
	"cache-control": "no-cache",
	"device-memory": "8",
	"sec-ch-dpr": "1.25",
	"dpr":"1.25",
	"rtt": "350",
	"downlink": "8.8",
	"ect": "4g",
	"sec-ch-ua": `".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"`,
	"sec-ch-ua-mobile": "?0",
	"sec-ch-ua-platform": "Windows",
	"upgrade-insecure-requests": "1",
	"sec-fetch-site": "same-origin",
	"sec-fetch-mode": "cors",
	"sec-fetch-dest": "empty",
	"viewport-width": "1536",
}

// https://ja3er.com/json
func NewAmazon(settings Settings) *Amazon {
	var client *mamba.Client
	if settings.Client != nil {
		client = settings.Client
	} else {
		client = mamba.NewWithRoundTripper(settings.JA3, settings.UserAgent)
	}

	amz := &Amazon{
		Base{
			client: client,
			//client:  mamba.NewWithRoundTripper(settings.JA3, settings.UserAgent),
			history: nil,
		},
	}

	amz.client.EnableTrace()
	amz.client.SetDebug(settings.Debug)
	amz.client.SetDebugBodyLimit(settings.DebugLength)

	if settings.Proxy != "" {
		switch settings.ProxyType {
		case "zyte":
			//amz.client.SetRootCertificate("D:\\goProject\\zyte-proxy-ca.crt")
			//amz.client.SetRestyProxy(settings.Proxy)
		default:
			amz.client.SetProxy(settings.Proxy, settings.UserAgent)

		}
	}

	//amz.client.SetProxy("http://192.168.2.7:8080", settings.UserAgent)
	amz.client.SetTimeout(3 * time.Minute)
	amz.client.SetRetryCount(20)
	amz.client.SetRetryWaitTime(3 * time.Second)
	headers := mergemap.Merge(defualtHeaders, settings.Header)
	for key, value := range headers {
		amz.client.SetHeader(key, value.(string))
	}
	amz.client.SetHeader("User-Agent", settings.UserAgent)

	amz.SetBaseUrl(settings.BaseUri)
	if settings.Cookies != nil {
		amz.SetCookies(settings.BaseUri, settings.Cookies)
	}

	amz.client.AddRetryCondition(func(response *mamba.Response, err error) bool {

		if err == nil {
			delete(response.Request.Header, "Cookie")
			currentPath := response.RawResponse.Request.URL.Path
			currentUrl := response.RawResponse.Request.URL.String()
			if strings.Contains(response.String(), `validateCaptcha`) {
				return false
			} else if currentPath == "/" { //防止无法抓取首页的token改变送货地址
				result := !strings.Contains(response.String(), "glowValidationToken")
				if result {
					fmt.Println("retry index")
				}

				return result
				//return !strings.Contains(response.String(), "glowValidationToken")
			} else if currentPath == "/portal-migration/hz/glow/get-rendered-address-selections" {
				//result := !strings.Contains(response.String(), "CSRF_TOKEN")
				//if result {
				//	fmt.Println("retry address")
				//}
				//return result
			} else if strings.Contains(currentUrl, "/dp/B0") && response.StatusCode() == 200 { // listing页面
				return !strings.Contains(response.String(), "(MEOW)")
			}

		} else {
			return true
		}

		return false
	})

	return amz
}
